﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VersionComparer.DataModel;

namespace VersionComparer.Comparer
{
    class ProgressManager
    {
        #region fields
        object lockObject = new object();
        int maxSteps = 20;
        private double fullPercentage = 0;
        #endregion fields

        
        #region constructors
        public ProgressManager(ComparisonEngine parent)
        {
            Parent = parent;
        }
        #endregion constructors

        #region properties
        ComparisonEngine Parent
        {
            get;
            set;
        }
        #endregion properties

        #region methods
        public void Reset()
        {
            fullPercentage = 0;
        }
        public void ItemCompleted(ComparableItem item)
        {

            int previousPercentage;
            int newPercentage;
            lock (lockObject)
            {
                previousPercentage = (int)(0.5 + fullPercentage * maxSteps);
                fullPercentage += item.Percentage;
                newPercentage = (int)(0.5 + fullPercentage * maxSteps);
            }
            if (newPercentage > previousPercentage)
            {
                Parent.OnUpdateProgress(newPercentage, maxSteps);
            }
        }
        #endregion methods

    }
}
